package cn.zyq.demo.system.cos.config;



import cn.zyq.demo.system.cos.AliyunCos;
import cn.zyq.demo.system.cos.CosService;
import cn.zyq.demo.system.cos.LocalCos;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
@EnableConfigurationProperties(StorageProperties.class)
public class StorageAutoConfiguration {

	private final StorageProperties properties;

	public StorageAutoConfiguration(StorageProperties properties) {
		this.properties = properties;
	}

	@Bean
	public CosService cosService() {
		CosService cosService = new CosService();
		String active = this.properties.getActive();
		cosService.setActive(active);
		if (active.equals("local")) {
			cosService.setStorage(localStorage());
		} else if (active.equals("aliyun")) {
			cosService.setStorage(aliyunStorage());
		// } else if (active.equals("tencent")) {
		// 	//暂时不提供
		// 	cosService.setStorage(tencentStorage());
		// } else if (active.equals("qiniu")) {
		// 	//暂时不提供
		// 	cosService.setStorage(qiniuStorage());
		} else {
			throw new RuntimeException("当前存储模式 " + active + " 不支持");
		}

		return cosService;
	}

	@Bean
	public LocalCos localStorage() {
		LocalCos localStorage = new LocalCos();
		StorageProperties.Local local = this.properties.getLocal();
		localStorage.setAddress(local.getAddress());
		localStorage.setStoragePath(local.getStoragePath());
		return localStorage;
	}

	@Bean
	public AliyunCos aliyunStorage() {
		AliyunCos aliyunStorage = new AliyunCos();
		StorageProperties.Aliyun aliyun = this.properties.getAliyun();
		aliyunStorage.setAccessKeyId(aliyun.getAccessKeyId());
		aliyunStorage.setAccessKeySecret(aliyun.getAccessKeySecret());
		aliyunStorage.setBucketName(aliyun.getBucketName());
		aliyunStorage.setEndpoint(aliyun.getEndpoint());
		return aliyunStorage;
	}

	// @Bean
	// public TencentCos tencentStorage() {
	// 	TencentCos tencentStorage = new TencentCos();
	// 	StorageProperties.Tencent tencent = this.properties.getTencent();
	// 	tencentStorage.setSecretId(tencent.getSecretId());
	// 	tencentStorage.setSecretKey(tencent.getSecretKey());
	// 	tencentStorage.setBucketName(tencent.getBucketName());
	// 	tencentStorage.setRegion(tencent.getRegion());
	// 	return tencentStorage;
	// }

	// @Bean
	// public QiniuCos qiniuStorage() {
	// 	QiniuCos qiniuStorage = new QiniuCos();
	// 	StorageProperties.Qiniu qiniu = this.properties.getQiniu();
	// 	qiniuStorage.setAccessKey(qiniu.getAccessKey());
	// 	qiniuStorage.setSecretKey(qiniu.getSecretKey());
	// 	qiniuStorage.setBucketName(qiniu.getBucketName());
	// 	qiniuStorage.setEndpoint(qiniu.getEndpoint());
	// 	return qiniuStorage;
	// }
}
